package com.oc.mba.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.DeviceMessageDAO;
import com.oc.mba.entity.DeviceMessageEntity;
import com.oc.mba.util.CommDateUtil;
import com.oc.mba.util.CommLog;

@Repository
public class DeviceMessageDAOImpl extends BaseDAOHibernate implements DeviceMessageDAO{
	
	private static final CommLog logger = new CommLog(DeviceMessageDAOImpl.class);
	
	public List<DeviceMessageEntity> findByProperty(String propertyName, Object value) {
		logger.debug("finding DeviceMessageEntity instance with property: " + propertyName + ", value: " + value);
		try {
			String hsql = "from DeviceMessageEntity as model where model." + propertyName + "= ? order by model.id";
			return getHibernateTemplate().find(hsql, value);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public void save(DeviceMessageEntity deviceMessageEntity) {
		logger.debug("save DeviceMessageEntity : " + deviceMessageEntity);
		try {
			getHibernateTemplate().save(deviceMessageEntity);
			logger.debug("save message success.");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("save DeviceMessageEntity failed.", e);
		}
	}

	@Override
	public void delete(Integer messageID) {
		logger.debug("delete DeviceMessageEntity. messageID=" + messageID);
		try {
			DeviceMessageEntity message = findMessage(messageID);
			if (message != null) {
				getHibernateTemplate().delete(message);
				logger.debug("delete message success.");
			}
			else {
				logger.debug("message doesn't exist.");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("delete DeviceMessageEntity failed.", e);
		}
	}

	@Override
	public DeviceMessageEntity findMessage(Integer messageID) {
		logger.debug("find DeviceMessageEntity. messageID=" + messageID);
		DeviceMessageEntity messageEntity = null;
		try {
			messageEntity = getHibernateTemplate().load(DeviceMessageEntity.class, messageID);
			if (messageEntity != null) {
				logger.debug("find message success. message exist.");
				return messageEntity;
			}
			else {
				logger.debug("find message success. message doesn't exist.");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("find DeviceMessageEntity failed.", e);
		}
		return messageEntity;
	}

	@Override
	public List<DeviceMessageEntity> findDeviceMessage(Integer deviceID, short msgType,
			Timestamp tNow, Timestamp tNext) {
		logger.debug("finding DeviceMessageEntity instance with create time between " + CommDateUtil.DateToString(tNow) + " to " + CommDateUtil.DateToString(tNext));
		try {
			String hsql = "from DeviceMessageEntity as model where model.deviceId=? and model.msgType=? and model.msgTime >= ? and model.msgTime < ?";
			return getHibernateTemplate().find(hsql, deviceID, msgType, tNow, tNext);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List<DeviceMessageEntity> findAllDeviceMessage(Integer deviceID) {
		logger.debug("finding DeviceMessageEntity instance with deviceID=" + deviceID);
		try {
			String hsql = "from DeviceMessageEntity as model where model.deviceId=? and model.msgFeedback=0";
			return getHibernateTemplate().find(hsql, deviceID);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

}
